<?php

/**
 *
 */
class user_game_logModel extends NewModel {

    public function multiAddLog($game_log_id, $result, $times, $podcast_id) {
        $create_time = NOW_TIME;
        $create_date = to_date($create_time, 'Y-m-d H:i:s');
        $table = DB_PREFIX . 'user_game_log';
        self::$sql = "INSERT INTO $table (
                `game_log_id`,
                `user_id`,
                `money`,
                `bet`,
                `podcast_id`,
                `create_time`,
                `create_date`,
                `type`
            ) SELECT
                '$game_log_id' AS `game_log_id`,
                `user_id`,
                (SUM(l.`money`) * $times) AS `money`,
                '0' AS `bet`,
                '$podcast_id' AS `podcast_id`,
                '$create_time' AS `create_time`,
                '$create_date' AS `create_date`,
                '2' AS `type`
            FROM
                $table AS l
            WHERE
                l.type = 1
            AND `game_log_id` = $game_log_id
            AND `bet` = $result
            GROUP BY
                `user_id`";
        return Connect::exec(self::$sql);
    }

    /**
     * @2017 红黑大战新增
     * @param type $game_log_id
     * @param type $result
     * @param type $times
     * @param type $podcast_id
     * @param type $game_id
     * @param type $wincardtype
     * @return type
     */
    public function multiAddLogForHongheiXyyj($game_log_id, $podcast_id, $game_id, $wincardtype) {
        $create_time = NOW_TIME;
        $create_date = to_date($create_time, 'Y-m-d H:i:s');
        $table = DB_PREFIX . 'user_game_log';
        self::debugmsg('user_game_logModel.class.php::multiAddLogForHongheiXyyj()[0]', compact('game_log_id', 'podcast_id', 'game_id', 'wincardtype'));
        if ($game_id != 5 || $wincardtype >= 5) {
            return true;
        }
        $huixinyiji = [0 => 15, 1 => 10, 2 => 4, 3 => 3, 4 => 2];
        $typetimes = $huixinyiji[$wincardtype];
        // 更新红黑大战幸运一击中奖结果
        self::$sql = "INSERT INTO $table (
                `game_log_id`,
                `user_id`,
                `money`,
                `bet`,
                `podcast_id`,
                `create_time`,
                `create_date`,
                `type`
            ) SELECT
                '$game_log_id' AS `game_log_id`,
                `user_id`,
                (SUM(l.`money`) * $typetimes) AS `money`,
                '0' AS `bet`,
                '$podcast_id' AS `podcast_id`,
                '$create_time' AS `create_time`,
                '$create_date' AS `create_date`,
                '2' AS `type`
            FROM
                $table AS l
            WHERE
                l.type = 1
            AND `game_log_id` = $game_log_id
            AND `bet` = 3
            GROUP BY
                `user_id`";
        self::debugmsg('user_game_logModel.class.php::multiAddLogForHongheiXyyj()[1]', self::$sql);
        return Connect::exec(self::$sql);
    }

    public function addLog($game_log_id, $podcast_id, $money, $user_id = false, $bet = 0, $type = 2) {
        $create_time = NOW_TIME;
        $create_date = to_date($create_time, 'Y-m-d H:i:s');
        if ($user_id === false) {
            $user_id = $podcast_id;
        }
        $data = array(
            'game_log_id' => $game_log_id,
            'user_id' => $user_id,
            'podcast_id' => $podcast_id,
            'money' => $money,
            'bet' => $bet,
            'create_time' => $create_time,
            'create_date' => $create_date,
            'type' => $type,
        );
        return $this->insert($data);
    }

    // @2017
    protected static function debugmsg($location, $msg) {
        $content = date('Y-m-d H:i:s') . PHP_EOL . $location . PHP_EOL . (is_string($msg) ? $msg : var_export($msg, true)) . PHP_EOL . PHP_EOL;
        $dir = APP_ROOT_PATH . 'java/logs/debug/';
        if (!is_dir($dir)) {
            mkdir($dir);
        }
        $path = $dir . 'debug-' . date('mdH') . '.txt';
        file_put_contents($path, $content, FILE_APPEND);
    }

}
